package com.zqq.demo.activity

import android.Manifest
import android.content.Intent
import android.os.Bundle
import com.zqq.base.BaseActivity
import com.zqq.base.permission.PermissionManager
import com.zqq.base.utils.StatusBarUtils
import com.zqq.demo.R
import com.zqq.demo.camera.CameraActivity
import com.zqq.demo.camera1.Camera2Activity
import com.zqq.demo.camera1.Camera3Activity
import com.zqq.demo.camera1.Camera4Activity
import com.zqq.demo.databinding.ActivityMainBinding
import com.zqq.demo.fragment.AFragment
import com.zqq.demo.fragment.BFragment
import com.zqq.demo.fragment.PFragment
import com.zqq.demo.viewmodel.MainViewModel


class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {

    lateinit var aFragment: AFragment
    lateinit var bFragment: BFragment
    lateinit var pFragment: PFragment

    override fun setContentViewId(): Int = R.layout.activity_main

    override fun init() {

        StatusBarUtils.setPaddingTopStatusBar(this,viewBinding.main)

//        viewModel.getList()
//
//        viewModel.url.observe(this){
//            pFragment?.changeUrl(it)
//        }
//
//        viewBinding.flB.setOnTouchListener { view, motionEvent ->
//            if(!PermissionManager.hasMediaPermission(this)){
//                PermissionManager.requestMediaPermission(this,1001)
//            }
//            true
//        }

        viewBinding.tvCamera.setOnClickListener {
            var hasPermission = PermissionManager.hasPermission(this, Manifest.permission.CAMERA)
            if(!hasPermission){
                PermissionManager.requestCameraPermission(this,1001)
                return@setOnClickListener
            }

            hasPermission = PermissionManager.hasPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            if(!hasPermission){
                PermissionManager.requestStorePermission(this,1001)
                return@setOnClickListener
            }

            hasPermission = PermissionManager.hasPermission(this, Manifest.permission.RECORD_AUDIO)
            if(!hasPermission){
                PermissionManager.requestAudioPermission(this,1001)
                return@setOnClickListener
            }

            startActivity(Intent(this, Camera4Activity::class.java))
        }
    }

    override fun initFragment(savedInstanceState: Bundle?) {
        super.initFragment(savedInstanceState)
        savedInstanceState?.let {
            it.hasFileDescriptors()
        }
        if(savedInstanceState != null){
            aFragment = supportFragmentManager.findFragmentByTag(AFragment::class.java.simpleName) as AFragment
            bFragment = supportFragmentManager.findFragmentByTag(BFragment::class.java.simpleName) as BFragment
            pFragment = supportFragmentManager.findFragmentByTag(PFragment::class.java.simpleName) as PFragment
        }else{
            aFragment = AFragment()
            bFragment = BFragment()
            pFragment = PFragment()

        }
        supportFragmentManager.beginTransaction()
            .add(R.id.fl_a, aFragment, AFragment::class.java.simpleName)
            .add(R.id.fl_b, bFragment, BFragment::class.java.simpleName)
            .add(R.id.fl_p, pFragment, PFragment::class.java.simpleName)
            .show(bFragment)
            .show(aFragment)
            .show(pFragment)
            .commit()

    }

}